package com.google.code.netbuilder.dal.entities;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.xml.bind.annotation.*;

@XmlRootElement(name = "wire")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "wire")
@SequenceGenerator(sequenceName = "wire_seq", name = "wire_seq", initialValue = 1, allocationSize = 100)
public class Wire extends ManagedObject {

	private static final long serialVersionUID = 6691757219954437108L;
	@XmlElement(name = "from_port")
    @XmlIDREF
    @OneToOne
    @JoinColumn(name = "from_port_id")
    private Port fromPort;
    @XmlElement(name = "to_port")
    @XmlIDREF
    @OneToOne
    @JoinColumn(name = "to_port_id")
    private Port toPort;
    @ManyToOne
    @JoinColumn(name = "cable_id")
    private Cable cable;

    public Port getFromPort() {
        return fromPort;
    }

    public void setFromPort(Port fromPort) {
        this.fromPort = fromPort;
    }

    public Port getToPort() {
        return toPort;
    }

    public void setToPort(Port toPort) {
        this.toPort = toPort;
    }

    public Cable getCable() {
        return cable;
    }

    public void setCable(Cable cable) {
        this.cable = cable;
    }
    
    @Override
    public boolean equals(Object ob)
    {
         if(ob == null)
        {
            return false;
        }
         
        if(!(ob instanceof Wire))
        {
            return false;
        }
        Wire castedOb = (Wire) ob;
        
        boolean fromCompare = false;
        boolean toCompare = false;
        
        if(fromPort == null || castedOb.getFromPort() == null)
        {
            fromCompare = (fromPort == castedOb.getFromPort());
        }
        else
        {
            fromCompare = (fromPort.getId() == castedOb.getFromPort().getId());
        }
        
        if(toPort == null || castedOb.getToPort() == null)
        {
            toCompare = (toPort == castedOb.getToPort());
        }
        else
        {
            toCompare = (toPort.getId() == castedOb.getToPort().getId());
        }
        return super.equals(ob) && fromCompare && toCompare; 
    }

    @Override
    public String toString() {
        return "Wire{" + super.toString() + ", fromPort=" + fromPort + ", toPort=" + toPort + '}';
    }
}
